/*
 * Copyright 2017 Baiyyy
 * @(#)IstrHandleExec.java
 */

package com.byyy.ccts.modules.cc.handle;

import com.byyy.ccts.modules.cc.Global;
import com.byyy.ccts.modules.cc.handle.istr.IHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/**
 * 初始化指令处理责任链
 * Created by DELL on 2018-4-27.
 */
@Service("istrHandleInit")
public class IstrHandleInit {

    @Autowired
    private IHandler loginIstrHdl;
    @Autowired
    private IHandler realTimeRptIstrHdl;
    @Autowired
    private IHandler suplReportIstrHdl;

    @Autowired
    private IHandler dRealTimeRptIstrHdl;
    @Autowired
    private IHandler dSuplReportIstrHdl;

    @Autowired
    private IHandler deviceRespIstrHdl;

    /**
     * 静态代码块，执行责任链初始化
     */
    static {
//        loginIstrHdl = new LoginIstrHdl();
//        realTimeRptIstrHdl = new RealTimeRptIstrHdl();
//        suplReportIstrHdl = new SuplReportIstrHdl();
//
//        loginIstrHdl.setNextHandle(realTimeRptIstrHdl);
//        realTimeRptIstrHdl.setNextHandle(suplReportIstrHdl);
    }

    /**
     * 将工程归于Spring管理，修改责任链初始化方式，放到init方法中
     */
    @PostConstruct
    public void init() {
        this.loginIstrHdl.setInstruction(Global.ISTR_LOGIN);
        this.loginIstrHdl.setNextHandle(this.realTimeRptIstrHdl);

        this.realTimeRptIstrHdl.setInstruction(Global.ISTR_REALTIME_REPORT);
        this.realTimeRptIstrHdl.setNextHandle(this.suplReportIstrHdl);

        this.suplReportIstrHdl.setInstruction(Global.ISTR_SUPPLEMENT_REPORT);
        this.suplReportIstrHdl.setNextHandle(this.dRealTimeRptIstrHdl);

        this.dRealTimeRptIstrHdl.setInstruction(Global.ISTR_D_REALTIME_REPORT);
        this.dRealTimeRptIstrHdl.setNextHandle(this.dSuplReportIstrHdl);

        this.dSuplReportIstrHdl.setInstruction(Global.ISTR_D_SUPPLEMENT_REPORT);
        this.dSuplReportIstrHdl.setNextHandle(this.deviceRespIstrHdl);

        this.deviceRespIstrHdl.setInstruction(Global.ISTR_DEVICE_RESPONSE);
    }

    /**
     * 返回初始链头
     * @return
     */
    public IHandler getInstance() {
        return this.loginIstrHdl;
    }

}
